<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="libman.css">
<TITLE>
Labeling
</TITLE>
</HEAD>
<BODY >
<A HREF="libman047.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="libman042.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="libman049.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc91">8.6</A>&nbsp;&nbsp;Labeling</H2>
In a constraint logic program, constraint handling is interleaved
with making choices. Typically, without making choices, constraint
problems cannot be solved completely. <EM>Labeling</EM><A NAME="@default260"></A>
is a controlled
way to make choices. Usually, a labeling predicate is called
at the end of the program which chooses values for the variables
constrained in the program. 
We will understand labeling in the most general sense as a procedure
introducing arbitrary choices (additional constraints on constrained
variables) in a systematic way.<BR>
<BR>
The <FONT COLOR=purple>CHR</FONT> run-time system provides <EM>built-in labeling</EM><A NAME="@default261"></A> for
user-defined constraints. The idea is to write clauses for
user-defined constraints that are used for labeling the variables in
the constraint. These clauses are not used during constraint handling,
but only during built-in labeling. Therefore the &#8220;Head&#8221; of a clause may
be a user-defined &#8220;Constraint&#8221;.
The <TT>label_with</TT> declaration<A NAME="@default262"></A> restricts the use of the 
clauses for built-in labeling (see subsection on declarations). There
can be several <TT>label_with</TT> declarations for a constraint.<BR>
<BR>
<B>Example, contd.:</B> <PRE CLASS="verbatim">
label_with minimum(X, Y, Z) if true.
minimum(X, Y, Z):- X leq Y, Z = X.
minimum(X, Y, Z):- Y lss X, Z = Y.
</PRE>
The built-in labeling is invoked by calling the <FONT COLOR=purple>CHR</FONT> built-in predicate
<TT>chr_labeling/0</TT> (no arguments). Once called, whenever no more
constraint handling is possible, the built-in labeling will choose a
constraint goal whose <TT>label_with</TT> declaration is satisfied for
labeling. It will introduce choices using the clauses of the constraint.<BR>
<BR>
<B>Example, contd.:</B>
A query without and with built-in labeling: <PRE CLASS="verbatim">
[eclipse]: minimum(X,Y,Z), maximum(X,Y,W), Z neq W.

X = _g357
Y = _g389
Z = _g421
W = _g1227
 
Constraints:
(1) minimum(_g357, _g389, _g421)
(2) _g421 leq _g357
(3) _g421 leq _g389
(4) maximum(_g357, _g389, _g1227)
(5) _g357 leq _g1227
(7) _g389 leq _g1227
(10) _g421 lss _g1227

yes.

[eclipse]: minimum(X,Y,Z), maximum(X,Y,W), Z neq W, chr_labeling.

X = Z = _g363
Y = W = _g395
 
Constraints:
(10) _g363 lss _g395

     More? (;) 

X = W = _g363
Y = Z = _g395

Constraints:
(17) _g395 lss _g363

yes.
</PRE>
Advanced users can write their own labeling procedure taking into
account the constraints in the constraint store (see next subsection
for <FONT COLOR=purple>CHR</FONT> built-in predicates to inspect and manipulate the constraint
store). <BR>
<BR>
<B>Example</B>
The predicate <TT>chr_labeling/0</TT> can be defined as: <PRE CLASS="verbatim">
        labeling :-
                chr_get_constraint(C),
                chr_label_with(C),
                !,
                chr_resolve(C),
                labeling.
        labeling.
</PRE>
<HR>
<A HREF="libman047.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="libman042.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="libman049.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
